# -*- coding:utf-8 -*-

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from process_bar import ProgressBar
from get_proxy import get_proxy
from multiprocessing import Process, Pool, Lock
import datetime
import sys
import requests
import json
import re

# key_words = [u'杏仁', u'饼干', u'薯片', u'麦片', u'牛肉干', u'巧克力', u'红酒',
#              u'伏特加', u'金酒', u'茅台', u'啤酒', u'牛奶', u'鸡蛋', u'猪肋骨', u'鱼', u'土鸡', u'牛排', u'苹果', u'橙子', u'速冻水饺',
#              u'NFC果汁', u'玉米油', u'米', u'盐', u'糖', u'酱油', u'醋', u'卷纸', u'抽纸', u'沐浴露', u'洗发水', u'洗洁精', u'洗衣粉',
#              u'香皂', u'保鲜膜', u'保鲜袋', u'卫生巾', u'牙膏', u'牙刷', u'毛巾']
key_words = [u'速冻水饺']
iter_key_words = None
res = [0] * len(key_words)
chromeOptions_list = []


def search_key_word(driver, key_word, index):
    print key_word
    process_bar = None
    global count_num
    elem_search = driver.find_element_by_name('q')
    elem_search.clear()
    elem_search.send_keys(key_word)
    elem_search.send_keys(Keys.RETURN)
    while True:
        if driver.title == u'理想生活上天猫':
            iframe = driver.find_element_by_tag_name('iframe')
            driver.switch_to_frame(iframe)
            elem_switch = driver.find_element_by_class_name('login-switch')
            elem_switch.click()
            elem_name = driver.find_element_by_name('TPL_username')
            elem_name.clear()
            elem_name.send_keys(u'追梦者梦在心中')
            elem_password = driver.find_element_by_name('TPL_password')
            elem_password.clear()
            elem_password.send_keys('19960221ykwan')
            elem_login = driver.find_element_by_id('J_SubmitStatic')
            elem_login.click()
            # driver.switch_to.parent_frame()
            # print driver.page_source
            driver.implicitly_wait(2)
        elif u'-天猫Tmall.com-理想生活上天猫' not in driver.title:
            wait = WebDriverWait(driver, 3600 * 24)
            element = driver.find_element_by_tag_name('div')
            # element.send_keys(Keys.COMMAND + 's')
            ActionChains(driver).context_click(element).perform()
            elem = wait.until(EC.title_contains(u'-天猫Tmall.com-理想生活上天猫'))
        elem_list = driver.find_element_by_id('J_ItemList')
        elem_products = elem_list.find_elements_by_class_name('product')
        for index_i, elem_product in enumerate(elem_products):
            # try:
                # count_num = elem_product.find_element_by_class_name('productStatus').find_element_by_tag_name('a').text
                # if u'万' in count_num:
                    # count_num = float(count_num.replace(u'万', '')) * 10000
                # res[index] += int(count_num)
            # except Exception, e:
                # print elem_product.text
                # pass
            while True:
                try:
                    count_num = get_comment_number_by_sku_and_store((elem_product.get_attribute('data-id'), 1))
                    print count_num, '=========', elem_product.get_attribute('data-id')
                    if count_num is None:
                        continue
                    else:
                        res[index] += int(count_num)
                        break
                except Exception, e:
                    print elem_product.get_attribute('data-id')
        elem_page_info = driver.find_element_by_class_name('ui-page-s-len').text.split('/')
        # print elem_page_info
        if len(elem_page_info) == 2:
            if process_bar is None:
                process_bar = ProgressBar(count=int(elem_page_info[0]), total=int(elem_page_info[1]))
            else:
                process_bar.move()
            process_bar.log()
            if elem_page_info[0] == elem_page_info[1]:
                break
        elif len(elem_page_info) == 1:
            break
        elem = driver.find_element_by_class_name('ui-page-next')
        elem.click()


def multiprocess_search(key, index):
    # global chromeOptions_list
    # chrome_option_index = 0
    # lock.acquire()
    # for index, i in enumerate(chromeOptions_list):
    #     print i
    #     if i[1]:
    #         chrome_option_index = index
    #         chrome_option = i
    #         chromeOptions_list[chrome_option_index] = (chrome_option[0], False)
    # lock.release()
    # init_driver = webdriver.Chrome(chrome_options=chrome_option[0])
    init_driver = webdriver.Chrome()
    init_driver.get('http://www.tmall.com')
    init_driver.maximize_window()
    print key, index
    search_key_word(init_driver, key, index)
    print res
    init_driver.quit()
    # chromeOptions_list[chrome_option_index] = (chrome_option[0], True)


def get_comment_number_by_sku_and_store(sku_store_tuple):
    url = 'https://rate.tmall.com/list_detail_rate.htm?itemId={item_id}&sellerId={seller_id}&currentPage=1&content=0'
    response = requests.get(url.format(item_id=sku_store_tuple[0], seller_id=sku_store_tuple[1]))
    s = response.text.find('\"total\"')
    total_str = str((response.text)[s: s + 20])
    pattern = re.compile(r'"total":(\d+),')
    match = pattern.match(total_str)
    if match:
        return match.group(1)


def write_res():
    reload(sys)
    sys.setdefaultencoding('gbk')
    date_now = datetime.datetime.now().strftime('../data/TMALL_%Y%m%d_%H%M%S.csv')
    file_out = open(date_now, 'w')
    file_out.write(','.join(key_words) + '\n')
    file_out.write(','.join([str(i) for i in res]) + '\n')
    file_out.close()


if __name__ == '__main__':
    # # 多进程版本
    # iter_key_words = iter([(key, index) for index, key in enumerate(key_words)])
    # proxy_ip_list = get_proxy('HTTP')
    # iter_proxy_ip = iter(proxy_ip_list)
    # for i in range(0, 4):
    #     ip_port = next(iter_proxy_ip)
    #     key_word = next(iter_key_words)
    #     chromeOptions = webdriver.ChromeOptions()
    #     chromeOptions.add_argument('--proxy-server=http://' + ip_port[0] + ':' + ip_port[1])
    #     chromeOptions_list.append((chromeOptions, True))
    # pool = Pool(processes=3)
    # # lock = Lock()
    # # print lock
    # for index, key in enumerate(key_words):
    #     pool.apply_async(multiprocess_search, (key, index, ))
    # pool.close()
    # pool.join()
    # write_res()

    # 标准版本
    # chrome_options = webdriver.ChromeOptions()
    # prefs = {"profile.managed_default_content_settings.images": 2}
    # chrome_options.add_experimental_option("prefs", prefs)

    # init_driver = webdriver.Chrome(chrome_options=chrome_options)
    init_driver = webdriver.Chrome()
    init_driver.get('http://www.tmall.com')
    init_driver.maximize_window()
    for index, key in enumerate(key_words):
        search_key_word(init_driver, key, index)
    init_driver.quit()
    write_res()
